home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 401-425 / disk_416 / utils / head / head.a < prev    next >
Text File  |  1992-05-06  |  3KB  |  179 lines

  1. ; head
  2.  
  3. ; (C) Stuart Mitchell - November 1990
  4.  
  5.         section head,code
  6.  
  7.         opt o+
  8.  
  9.         incdir "include:"
  10.  
  11.         INCLUDE "include:exec/types.i"
  12.         INCLUDE "include:exec/memory.i"
  13.         INCLUDE "include:libraries/dos.i"
  14.         include "libraries/arpbase.i"
  15.  
  16. NUM_LINES    equ    10
  17. LF        equ    $0a
  18.  
  19. _LVOOpenLibrary     equ    -$228
  20. _LVOCloseLibrary    equ    -$19e
  21.  
  22. CALL    MACRO
  23.     jsr    _LVO\1(a6)
  24.     ENDM
  25.  
  26. r_current_char    equr    a2
  27. r_buffer    equr    a3
  28. r_arpbase    equr    a6            ; must be a6
  29.  
  30. r_num_arg    equr    d7
  31. r_fh        equr    d6
  32. r_lines     equr    d5
  33. r_cli_fh    equr    d4
  34. r_num_read    equr    d3            ; don't move this one
  35. r_count     equr    d2
  36.  
  37.  
  38. start:        move.l    4.w,a6            ;transfer ExecBase
  39.  
  40. ; save command line - d4/a4 won't be trashed by OpenLibrary call...
  41.  
  42.         move.l    a0,a4            ;address of startup line
  43.         move.l    d0,d4            ;length
  44.  
  45. ; Open arp.library
  46.  
  47.         lea    arpname(pc),a1          ;get address of arpname
  48.         moveq.l #39,d0            ;version 1.3+
  49.         CALL    OpenLibrary
  50.         move.l    d0,r_arpbase
  51.         beq    close            ;if NULL exit
  52.  
  53. ; parse filename
  54.  
  55.         move.l    a4,a0            ; string
  56.         move.l    d4,d0            ; length
  57.  
  58.         lea    help(pc),a1
  59.         lea    arg0(pc),a2             ; pointer to arg array
  60.         lea    tplate(pc),a3
  61.         CALL    GADS
  62.         tst.l    d0
  63.         beq    pr_usage
  64.         blt    pr_line_err
  65.  
  66. ; find output
  67.  
  68.         CALL    Output
  69.         move.l    d0,r_cli_fh
  70.  
  71. ; allocate buffer of 512 bytes of memory (MEMF_PUBLIC|MEMF_CLEAR)
  72.  
  73.         move.l    #512,d0
  74.         CALL    ArpAlloc
  75.         tst.l    d0
  76.         beq    pr_nomem        ;error if returned NULL
  77.         move.l    d0,r_buffer        ;save address
  78.  
  79. ; open file
  80.  
  81. open_file:    move.l    arg1,d1         ;pointer to filename
  82.         move.l    #MODE_OLDFILE,d2
  83.         CALL    ArpOpen
  84.         beq.s    pr_nofile        ;if fh==0 exit
  85.         move.l    d0,r_fh         ;save file handle
  86.  
  87. ; set up count of lines to print
  88.  
  89.         move.l    arg0,d0
  90.         beq.s    ten            ; if no arg then default
  91.         move.l    d0,a0
  92.         CALL    Atol
  93.         beq    bad_num
  94.         tst.l    d0
  95.         ble.s    bad_num
  96.         move.l    d0,r_lines
  97.         bra.s    read512
  98.  
  99. ten        moveq.l #NUM_LINES,r_lines    ;print 10 lines
  100.  
  101. ; read in 512 bytes of file to buffer
  102.  
  103. read512     move.l    #512,d3            ;read 512 bytes
  104.         move.l    r_buffer,d2        ;into allocated mem
  105.         move.l    r_fh,d1         ;from fh
  106.         CALL    Read
  107.         beq.s    close            ;if no chars (EOF) then exit
  108.         move.l    d0,r_num_read        ;save num chars read
  109.         move.l    r_buffer,r_current_char ;move to start buffer
  110.  
  111.         moveq.l #0,r_count        ;clear count of chars
  112.  
  113. loop        addq.l    #1,r_count        ;inc number chars read
  114.         cmpi.b    #LF,(r_current_char)+   ;if not (char==LF)
  115.         bne.s    cont
  116.         subq    #1,r_lines        ;decrement lines count
  117.         beq.s    finished        ;branch if finished
  118. cont        cmp.l    r_count,r_num_read    ;if not buffer empty
  119.         bne.s    loop            ;do next char
  120.  
  121.         move.l    r_cli_fh,d1
  122.         move.l    r_buffer,d2        ;print all (d3) num of chars
  123.         CALL    Write,a5
  124.  
  125.         bra.s    read512         ;read next lot
  126.  
  127. finished    move.l    r_count,d3        ;print out num required
  128.         move.l    r_buffer,d2
  129.         move.l    r_cli_fh,d1
  130.         CALL    Write,a5        ;of characters
  131.  
  132. ; Arp frees memory & closes files automatically
  133.  
  134. close        move.l    r_arpbase,a1        ;move pointer to arp
  135.         move.l    4.w,a6
  136.         CALL    CloseLibrary        ;close arp.library
  137.  
  138. exit_dos:    moveq.l #0,d0
  139.         rts
  140.  
  141. pr_nofile    lea    nofile(pc),a0
  142.         lea    arg1(pc),a1
  143.         CALL    Printf
  144.         bra.s    close
  145.  
  146. pr_nomem    lea    mem(pc),a1
  147.         CALL    Puts
  148.         bra.s    close
  149.  
  150. pr_usage    lea    help(pc),a1
  151.         CALL    Puts
  152.         bra.s    close
  153.  
  154. pr_line_err    move.l    arg0,a1        ; arg0 set to some string by ARP
  155.         CALL    Puts
  156.         bra.s    close
  157.  
  158. bad_num     lea    num(pc),a1
  159. my_puts:    CALL    Puts
  160.         bra.s    close
  161.  
  162.         even
  163. arpname     dc.b    'arp.library',0
  164.         even
  165. help        dc.b    'usage : head [lines <num lines>] <file>',0
  166.         even
  167. tplate        dc.b    'l=lines/K,file/A',0
  168.         even
  169. arg0        dc.l    0
  170. arg1        dc.l    0
  171.         even
  172. mem        dc.b    'No memory',0
  173.         even
  174. nofile        dc.b    'Cannot open file "%s"',$a,0
  175.         even
  176. num        dc.b    'Illegal lines count',0
  177.         end
  178.  
  179.